package zdh.avatar.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;


public final class ElementLoader {

    private static ObjectOutputStream output;
    private static ObjectInputStream input;

/* -----------------------------------------------------------------------------
 *                      ELEMENT PANEL METHODS
 * -----------------------------------------------------------------------------
 */
    
    public static boolean addElementPanel(ElementPanel pPanel, 
                                        String elementPanelPath, 
                                        boolean overwrite) {
        File panelDir = new File(elementPanelPath);
        if (!panelDir.exists()) {
            System.out.println("Creating panel directory");
            panelDir.mkdirs();
        }

        if (panelDir.isDirectory()) {
            String properName = Util.getProperFilename(pPanel.getPanelName());
            String filename = elementPanelPath + properName + ".element";

            if (!overwrite) {
                File file = new File(filename);
                if (file.exists()) {
                    return false;
                }
            }

            try {
                output = new ObjectOutputStream(new FileOutputStream(filename));
                output.writeObject(pPanel);
                output.close();
                return true;
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
        }
        return false;
    }

    public static void deleteElementPanel(ElementPanel pPanel, String elementPanelPath) {
        String properName = Util.getProperFilename(pPanel.getPanelName());
        String pFile = elementPanelPath + properName + ".element";
        File f = new File(pFile);
        if (f.exists()) {
            f.delete();
        }
    }

    public static void replaceElementPanel(ElementPanel oldPanel, 
                                        ElementPanel newPanel, 
                                        String elementPath) {
        
        ArrayList<ElementButton> buttons = getElementButtons
                                        (oldPanel.getPanelName(), elementPath + "buttons/");
        // move buttons to new panel
        if (buttons != null) {
            for (ElementButton pb : buttons) {
                ElementButton npb = new ElementButton
                                        (pb.getButtonName(), 
                                        pb.getButtonColor(), 
                                        pb.getAudioPaths(), 
                                        newPanel.getPanelName(),
                                        pb.getButtonIndex());
                replaceElementButton(pb, npb, elementPath + "buttons/");
            }
        }

        // replace the panel
        deleteElementPanel(oldPanel, elementPath + "panels/");
        addElementPanel(newPanel, elementPath + "panels/", false);
    }

    public static ElementPanel[] getElementPanels(String elementPanelPath) {
        File file = new File(elementPanelPath);
        ElementPanel[] elementPanels = null;

        if (file.isDirectory()) {
            String[] files = file.list();
            if (files.length > 0) {
                elementPanels = new ElementPanel[files.length];
                for (int i = 0; i < files.length; i++) {
                    if (files[i].contains(".element")) {
                        try {
                            String filePath = elementPanelPath + files[i];
                            input = new ObjectInputStream(new FileInputStream(filePath));
                            elementPanels[i] = (ElementPanel) input.readObject();
                            input.close();
                        } catch (IOException | ClassNotFoundException e) {
                            System.err.println(e.getMessage());
                        }
                    }
                }// end for loop
            }// end if has file in directory
        }// end if path is a directory

        /*
         * Sort the panels according to their tab index...
         */
        if (elementPanels != null && elementPanels.length > 1) {
            for (int i = 0; i < elementPanels.length-1; i++) {
                for (int j = i; i < elementPanels.length; i++) {
                    if (elementPanels[j].getTabIndex() < elementPanels[j + 1].getTabIndex()) {
                        break;
                    }

                    ElementPanel temp = elementPanels[j];
                    elementPanels[j] = elementPanels[j + 1];
                    elementPanels[j + 1] = temp;
                }
            }
        }

        return elementPanels;
    }
    
    public static int getElementPanelCount(String elementPanelPath){
        File file = new File(elementPanelPath);
        if(file.exists() && file.isDirectory()){
            return file.list().length;
        }
        return 0;
    }

/* -----------------------------------------------------------------------------
 *                      ELEMENT BUTTON METHODS
 * -----------------------------------------------------------------------------
 */
    
    public static boolean addElementButton(ElementButton pButton, 
                                        String elementButtonPath, 
                                        boolean overwrite) {
        File buttonsDir = new File(elementButtonPath);
        if (!buttonsDir.exists()) {
            buttonsDir.mkdirs();
        }

        if (buttonsDir.isDirectory()) {
            String properName = Util.getProperFilename(pButton.getButtonName());
            String filename = elementButtonPath + properName + ".element";

            File file = new File(filename);
            if (file.exists()) {
                return false;
            }

            try {
                output = new ObjectOutputStream(new FileOutputStream(filename));
                output.writeObject(pButton);
                output.close();
                return true;
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
        }
        return false;
    }

    public static ElementButton[] getElementButtons(String elementButtonPath) {
        File file = new File(elementButtonPath);
        ElementButton[] elementButtons = null;

        if (file.isDirectory()) {
            String[] files = file.list();
            if (files.length > 0) {
                elementButtons = new ElementButton[files.length];
                for (int i = 0; i < files.length; i++) {
                    if (files[i].contains(".element")) {
                        try {
                            String path = elementButtonPath + files[i];
                            input = new ObjectInputStream(new FileInputStream(path));
                            elementButtons[i] = (ElementButton) input.readObject();
                            input.close();
                        } catch (IOException | ClassNotFoundException e) {
                        }
                    }
                }// end for loop
            }// end if has files in directory
        }// end if path is a directory

        return elementButtons;
    }

    public static ArrayList<ElementButton> getElementButtons(String panelName, String elementButtonPath) {
        ArrayList<ElementButton> btns = new ArrayList<>();
        File dir = new File(elementButtonPath);
        if (dir.exists()) {
            ElementButton[] _btns = getElementButtons(elementButtonPath);
            if (_btns != null) {
                if (_btns.length > 0) {
                    for (int i = 0; i < _btns.length; i++) {
                        ElementButton pb = _btns[i];
                        if (pb.getPanelName().equals(panelName)) {
                            btns.add(pb);
                        }
                    }
                }
                return btns;
            }
            return null; // no buttons found
        }
        return null; // directory doesn't exists
    }

    public static void deleteElementButton(ElementButton elementButton, String elementButtonPath) {
        String properName = Util.getProperFilename(elementButton.getButtonName());
        File file = new File(elementButtonPath + properName + ".element");
        if (file.exists()) {
            file.delete();
        }
    }

    public static void replaceElementButton(ElementButton oldElementButton, ElementButton newElementButton, String elementButtonPath) {
        deleteElementButton(oldElementButton, elementButtonPath);
        addElementButton(newElementButton, elementButtonPath, false);
    }
}
